FSORTM.BIN is an external assembly language program for use with Turbo Pascal that gives very high speed sort capability on multiple keys. It for sure runs with Turbo Pascal version 3.01A (the latest version Borland has released) and should run with the earlier versions, provided the bug on the the size of external assembly language programs is worked around.
Usage:
const
MAXKEYS = (however many keys you will use) ;
type
KeyType = array[1..MAXKEYS] of integer ;
procedure Sort(RecLen : integer ; {Size of each array record }
NumKeys : integer ; {Number of sort keys being used}
var KeyPos : KeyType ; {Array of sort key positions }
var KeyLen : KeyType ; {Array of sort key lengths }
RecCnt : integer ; {Number of records to sort }
Base : integer ) ; {Segment address of array }
external 'FSORTM.BIN' ;
IMPORTANT NOTE:
This version of FSORTM is designed for use with arrays created on Turbo's
Heap using the "New" or "GetMem" procedures. It is NOT designed to be used
with arrays that are defined in Turbo Pascal's Data Segment, or anywhere
where the array to be sorted cannot for sure be made to start on a paragraph
(16 byte) boundary in the 8088/8086 address space. This is a compromise that
permits very high speed sorting by cutting down on the address computation.
For the same reason, the size of RecLen MUST be a multiple of 16 bytes
otherwise unpredictable happenings will occur during the sort. The sort
routine depends for its operation on the records being adressable with the
just 8088 segment registers. As a result the base address of the array
must have an offset divisible by 16, i.e. the address of the array must be
on a paragraph (16 byte) boundary in the 8088 adress space. This is guaranteed
by the "New" procedure in Turbo Pascal if the sort array is the FIRST variable
allocated memory space on the heap.
Although not covered in the Turbo Pascal Reference Manual the minimum
allocation unit that either "New" or "GetMem" uses is 8 bytes. Therefore, byte allocations on Turbo's heap go like this:
Variable Size in bytes Allocation in bytes
---------------------- -------------------
1 - 8 8
9 - 16 16
17 - 24 24
25 - 32 32
etc. etc.
So, the current offset of the predefined variable "HeapPtr" can be checked
to see if it zero, and if it isn't, allocate a pointer to type byte before
allocating for the array to be sorted. This would only be needed if there had
been a previous use of "GetMem" and/or "New" in the program. It can easily